package com.lanyou.cook.entity.domain;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.datadictionary.DBType;
import com.lanyou.cook.datadictionary.SystemInterfaceScene;
import com.lanyou.cook.datadictionary.SystemInterfaceTecStandard;
import com.lanyou.cook.entity.kettle.KettleJob;
import com.lanyou.cook.service.DataDictionaryService;

@Entity
@EntityListeners({ AuditingEntityListener.class})
@Table(name = "T_COOK_BU_FIELD_JOB")
public class SystemInterface {

	private String id;

	private int version;

	private String jobName;
	
	private String jobCode;

	private String tecStandard;

	private String senderDBType;

	private String receiverDBType;
	
	private String sendKettleDataSource;

	private String receiverKettleDataSource;

	private String interfaceScene;

	private String interfaceDesc;

	private String remark;

	private String creator;

	private String modifier;

	private Date createTime;

	private Date modifyTime;

	private FieldSystem ownerFieldSystem;

	private FieldSystem senderFieldSystem;

	private FieldSystem receiverFieldSystem;

	private KettleJob kettleJob;
	
	@Column(name="JOB_CODE",length=50)
	public String getJobCode() {
		return jobCode;
	}

	public void setJobCode(String jobCode) {
		this.jobCode = jobCode;
	}
	@Column(name="SENDER_DATA_SOURCE",length=50)
	public String getSendKettleDataSource() {
		return sendKettleDataSource;
	}

	public void setSendKettleDataSource(String sendKettleDataSource) {
		this.sendKettleDataSource = sendKettleDataSource;
	}
	
	@Column(name="RECEIVER_DATA_SOURCE",length=50)
	public String getReceiverKettleDataSource() {
		return receiverKettleDataSource;
	}

	public void setReceiverKettleDataSource(String receiverKettleDataSource) {
		this.receiverKettleDataSource = receiverKettleDataSource;
	}
	public final static Specification<SystemInterface> EmptySpecification = new Specification<SystemInterface>(){
		@Override
		public Predicate toPredicate(Root<SystemInterface> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<SystemInterface> idSpecification(String id){
		return new Specification<SystemInterface>(){
			@Override
			public Predicate toPredicate(Root<SystemInterface> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}

	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "FIELD_JOB_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Version
	@JsonIgnore
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	@Column(name = "JOB_NAME", length = 50)
	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	@Column(name = "TECH_STANDARD", length = DataDictionaryValue.MAX_LENGTH_OF_CODE)
	public String getTecStandard() {
		return tecStandard;
	}

	public void setTecStandard(String tecStandard) {
		this.tecStandard = tecStandard;
	}

	@Column(name = "SENDER_DB_TYPE", length = DataDictionaryValue.MAX_LENGTH_OF_CODE)
	public String getSenderDBType() {
		return senderDBType;
	}

	public void setSenderDBType(String senderDBType) {
		this.senderDBType = senderDBType;
	}

	@Column(name = "RECEIVER_DB_TYPE", length = DataDictionaryValue.MAX_LENGTH_OF_CODE)
	public String getReceiverDBType() {
		return receiverDBType;
	}

	public void setReceiverDBType(String receiverDBType) {
		this.receiverDBType = receiverDBType;
	}

	@Column(name = "INTERFACE_SCENE", length = DataDictionaryValue.MAX_LENGTH_OF_CODE)
	public String getInterfaceScene() {
		return interfaceScene;
	}

	public void setInterfaceScene(String interfaceScene) {
		this.interfaceScene = interfaceScene;
	}

	@Column(name = "INTERFACE_DESC", length = 100)
	public String getInterfaceDesc() {
		return interfaceDesc;
	}

	public void setInterfaceDesc(String interfaceDesc) {
		this.interfaceDesc = interfaceDesc;
	}

	@Column(length = 100)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@CreatedBy
	@Column(length = 50, updatable = false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable = false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@ManyToOne
	@JoinColumn(name = "FIELD_SYSTEM_ID")
	public FieldSystem getOwnerFieldSystem() {
		return ownerFieldSystem;
	}

	public void setOwnerFieldSystem(FieldSystem ownerFieldSystem) {
		this.ownerFieldSystem = ownerFieldSystem;
	}

	@ManyToOne
	@JoinColumn(name = "IF_SENDER")
	public FieldSystem getSenderFieldSystem() {
		return senderFieldSystem;
	}

	public void setSenderFieldSystem(FieldSystem senderFieldSystem) {
		this.senderFieldSystem = senderFieldSystem;
	}

	@ManyToOne
	@JoinColumn(name = "IF_RECEIVER")
	public FieldSystem getReceiverFieldSystem() {
		return receiverFieldSystem;
	}

	public void setReceiverFieldSystem(FieldSystem receiverFieldSystem) {
		this.receiverFieldSystem = receiverFieldSystem;
	}

	@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE, CascadeType.DETACH})
	@JoinColumn(name = "R_JOB_ID")
	public KettleJob getKettleJob() {
		return kettleJob;
	}

	public void setKettleJob(KettleJob kettleJob) {
		this.kettleJob = kettleJob;
	}

	@Transient
	public String getFieldSystemName() {
		try {
			return (String) PropertyUtils.getProperty(ownerFieldSystem, "name");
		} catch (Exception e) {
			return null;
		}
	}

	@Transient
	public String getFieldName() {
		try {
			return (String) PropertyUtils.getProperty(ownerFieldSystem, "field.name");
		} catch (Exception e) {
			return null;
		}
	}

	@Transient
	public String getSenderFieldSystemId() {
		try {
			return (String) PropertyUtils.getProperty(senderFieldSystem, "id");
		} catch (Exception e) {
			return null;
		}
	}

	@Transient
	public String getSenderFieldSystemName() {
		try {
			return (String) PropertyUtils.getProperty(senderFieldSystem, "name");
		} catch (Exception e) {
			return null;
		}
	}

	@Transient
	public String getSenderFieldSystem_fieldId() {
		try {
			return (String) PropertyUtils.getProperty(senderFieldSystem, "field.id");
		} catch (Exception e) {
			return null;
		}
	}

	@Transient
	public String getSenderFieldSystem_fieldName() {
		try {
			return (String) PropertyUtils.getProperty(senderFieldSystem, "field.name");
		} catch (Exception e) {
			return null;
		}
	}

	@Transient
	public String getReceiverFieldSystemId() {
		try {
			return (String) PropertyUtils.getProperty(receiverFieldSystem, "id");
		} catch (Exception e) {
			return null;
		}
	}

	@Transient
	public String getReceiverFieldSystemName() {
		try {
			return (String) PropertyUtils.getProperty(receiverFieldSystem, "name");
		} catch (Exception e) {
			return null;
		}
	}

	@Transient
	public String getReceiverFieldSystem_fieldId() {
		try {
			return (String) PropertyUtils.getProperty(receiverFieldSystem, "field.id");
		} catch (Exception e) {
			return null;
		}
	}

	@Transient
	public String getReceiverFieldSystem_fieldName() {
		try {
			return (String) PropertyUtils.getProperty(receiverFieldSystem, "field.name");
		} catch (Exception e) {
			return null;
		}
	}

	// 接口场景前端显示处理
	@Transient
	public String getInterfaceSceneForDisplay() {
		return DataDictionaryService.getInstance().getValueName(SystemInterfaceScene.TypeCode, interfaceScene);
	}

	// 技术标准前端显示处理
	@Transient
	public String getTecStandardForDisplay() {
		return DataDictionaryService.getInstance().getValueName(SystemInterfaceTecStandard.TypeCode, tecStandard);
	}

	// 发送方数据库类型前端显示处理
	@Transient
	public String getSenderDBTypeForDisplay() {
		return DataDictionaryService.getInstance().getValueName(DBType.TypeCode, senderDBType);
	}

	// 接收方数据库前端显示处理
	@Transient
	public String getReceiverDBTypeForDisplay() {
		return DataDictionaryService.getInstance().getValueName(DBType.TypeCode, receiverDBType);
	}

	// public static Specification<SystemInterface> idSpecification(String id){
	// return new Specification<SystemInterface>(){
	// @Override
	// public Predicate toPredicate(Root<SystemInterface> root, CriteriaQuery<?>
	// query, CriteriaBuilder cb) {
	// query.where(cb.equal(root.get("id"), id));
	// return null;
	// }
	// };
	// }
}
